﻿/*
 ***************************************************************************
 * Microsoft Visual C# .NET
 * 名称：       App_Input_LivingMemberLivingForm
 * 类型：       控制层
 * 功能说明：   生活管家-会员生活列表页面
 * 作者：       黄鑫
 * 创建日期：   2007/11/13
 * 修改日期     修改者      修改内容
 * 
 * 
 * ---
 ***************************************************************************
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Input.Dao;
using Shpm.App.Input.Bean;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.System;
using log4net;
using Shpm.App.Commons.Utils;
using Shpm.System.Validator;
using Shpm.System.Web.Control;

public partial class App_Input_LivingMemberLivingForm : System.Web.UI.Page
{
    private const string LIVING_COUNT = "LivingCount";
    private const string FEE_COUNT = "FeeCount";

    // log
    protected static ILog log = LogManager.GetLogger(typeof(App_Input_LivingMemberLivingForm));

    // page load
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            // 取得当前登录的用户id信息
            SysUserManager manager = new SysUserManager(Context);
            SysUserBean sysUserBean = manager.getSysUserBean();
            MstSysUserBean mstSysUserBean =
                sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
            MstEmployeeBean mstEmpBean =
                sysUserBean == null ? null : sysUserBean.getEmployeeBean();
            string loginId = mstSysUserBean == null ? "" : mstSysUserBean.SysUserId;
            string emplyId = mstSysUserBean == null ? "" : mstSysUserBean.EmployeeId;
            string emplyJob = mstSysUserBean == null ? "" : mstEmpBean.EmployeeJob;
            string emplyLevel = mstSysUserBean == null ? "" : mstEmpBean.EmployeeLevel;
            string emplyRole = mstEmpBean == null ? "" : mstEmpBean.Role;
            ViewState["USER_ID"] = emplyId;
            if (Context.Items.Contains("MEMBER_NO"))
            {
                this.ViewState["MEMBER_NO"] = Context.Items["MEMBER_NO"];
            }
            else
            {
                this.ViewState["MEMBER_NO"] = "A0010010101";
            }
            if (Context.Items.Contains("URL"))
            {
                this.ViewState["URL"] = Context.Items["URL"];
            }
            else
            {
                this.ViewState["URL"] = null;
            }
        }
        else
        {
            // 恢复配置控件（动态）
            RestoreItemControls();
        }
    }

    /*名称： RestoreItemControls
     *功能： 恢复房间配置控件
     *参数： 无
     *返回： 无
     *说明： 无
     */
    protected void RestoreItemControls()
    {
        if (ViewState.IsItemDirty(LIVING_COUNT))
        {
            int livingCount = (int)ViewState[LIVING_COUNT];

            while (TblLiving.Rows.Count > 1)
            {
                TblLiving.Rows.RemoveAt(1);
            }

            for (int i = 0; i < livingCount; i++)
            {
                TableRow tblRow = new TableRow();

                TableCell tblCell;
                CheckBox chkUse = new CheckBox();
                chkUse.ID = string.Format("chkChoose_{0}", i);
                tblCell = new TableCell();
                tblCell.Controls.Add(chkUse);
                tblRow.Cells.Add(tblCell);

                tblCell = new TableCell();
                tblRow.Cells.Add(tblCell);

                tblCell = new TableCell();
                tblRow.Cells.Add(tblCell);

                tblCell = new TableCell();
                tblRow.Cells.Add(tblCell);

                tblCell = new TableCell();
                tblRow.Cells.Add(tblCell);

                TextBox txtPrice = new TextBox();
                 txtPrice.ID = string.Format("txtPrice_{0}", i);
                 txtPrice.Attributes.Add("onblur", "sumamount('" + i + "')");
                HiddenField hidItemId = new HiddenField();
                hidItemId.ID = string.Format("hidItemId_{0}", i);

                tblCell = new TableCell();
                tblCell.Controls.Add(hidItemId);
                tblCell.Controls.Add(txtPrice);
                tblRow.Cells.Add(tblCell);

                tblCell = new TableCell();
                TextBox txtMultiple = new TextBox();
                txtMultiple.ID = string.Format("txtMultiple_{0}", i);
                txtMultiple.Attributes.Add("onblur", "sumamount('" + i + "')");
                tblCell.Controls.Add(txtMultiple);
                tblRow.Cells.Add(tblCell);

                tblCell = new TableCell();
                TextBox txtAmount = new TextBox();
                txtAmount.ID = string.Format("txtAmount_{0}", i);
                tblCell.Controls.Add(txtAmount);
                tblRow.Cells.Add(tblCell);

                TblLiving.Rows.Add(tblRow);
            }
        }

        if (ViewState.IsItemDirty(FEE_COUNT))
        {
            int feeCount = (int)ViewState[FEE_COUNT];

            while (TblFee.Rows.Count > 1)
            {
                TblFee.Rows.RemoveAt(1);
            }

            for (int i = 0; i < feeCount; i++)
            {
                TableRow tblRow = new TableRow();

                TableCell tblCell;
                CheckBox chkUse = new CheckBox();
                chkUse.ID = string.Format("chkChooseFee_{0}", i);
                tblCell = new TableCell();
                tblCell.Controls.Add(chkUse);
                tblRow.Cells.Add(tblCell);


                tblCell = new TableCell();
                tblRow.Cells.Add(tblCell);

                tblCell = new TableCell();
                tblRow.Cells.Add(tblCell);

                TextBox txtPriceFee = new TextBox();
                txtPriceFee.ID = string.Format("txtPriceFee_{0}", i);
                txtPriceFee.Attributes.Add("onblur", "sumamountfee('" + i + "')");

                HiddenField hidItemIdFee = new HiddenField();
                hidItemIdFee.ID = string.Format("hidItemIdFee_{0}", i);

                tblCell = new TableCell();
                tblCell.Controls.Add(hidItemIdFee);
                tblCell.Controls.Add(txtPriceFee);
                tblRow.Cells.Add(tblCell);

                tblCell = new TableCell();
                TextBox txtMultipleFee = new TextBox();
                txtMultipleFee.ID = string.Format("txtMultipleFee_{0}", i);
                txtMultipleFee.Attributes.Add("onblur", "sumamountfee('" + i + "')");
                tblCell.Controls.Add(txtMultipleFee);
                tblRow.Cells.Add(tblCell);

                tblCell = new TableCell();
                TextBox txtAmountFee = new TextBox();
                txtAmountFee.ID = string.Format("txtAmountFee_{0}", i);
                tblCell.Controls.Add(txtAmountFee);
                tblRow.Cells.Add(tblCell);

                TblFee.Rows.Add(tblRow);
            }
        }

    }

    /*名称： ClearItemControls
     *功能： 清空房间配置控件
     *参数： 无
     *返回： 无
     *说明： 无
     */
    protected void ClearItemControls()
    {
        if (ViewState.IsItemDirty(LIVING_COUNT))
        {
            int livingCount = (int)ViewState[LIVING_COUNT];

            while (TblLiving.Rows.Count > 1)
            {
                TblLiving.Rows.RemoveAt(1);
            }

            ViewState[LIVING_COUNT] = 0;
        }

        if (ViewState.IsItemDirty(FEE_COUNT))
        {
            int feeCount = (int)ViewState[FEE_COUNT];

            while (TblFee.Rows.Count > 1)
            {
                TblFee.Rows.RemoveAt(1);
            }

            ViewState[FEE_COUNT] = 0;
        }

    }


    /*名称： OnPreRender
     *功能： 此方法通知服务器控件在保存视图状态和呈现内容之前，执行任何必要的预呈现步骤。
     *参数： e - 包含事件数据的 EventArgs 对象
     *返回： 无
     *说明： 无
     */
    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);

        // 清空配置控件（动态）
        ClearItemControls();

        // get current doctor responsibility members list
        LivingMemberLivingDao dao = new LivingMemberLivingDao();
        LivingMemberLivingBean beanLiving = new LivingMemberLivingBean();
        LivingMemberFeeBean beanFee = new LivingMemberFeeBean();
        if(this.ViewState["MEMBER_NO"] == null)
        {
            ((Design_MasterPage)this.Master).InfoMessage =
                    HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0019").ToString();
            return;
        }
        beanLiving.MemberNo = Convert.ToString(this.ViewState["MEMBER_NO"]);
        IList lstLiving = dao.ListLiving(beanLiving);
        if (lstLiving.Count == 0)
        {
        }
        else
        {
            ViewState[LIVING_COUNT] = lstLiving.Count;
            int i = 0;
            foreach(LivingMemberLivingBean bean in lstLiving)
            {
                TableRow row = new TableRow();
                TableCell cell = new TableCell();

                cell = new TableCell();
                CheckBox chkChoose = new CheckBox();
                chkChoose.Checked = (Boolean)bean.ChooseFlg;
                chkChoose.ID = string.Format("chkChoose_{0}", i);
                cell.Controls.Add(chkChoose);
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Controls.Add(new LiteralControl(bean.BeginTime));
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Controls.Add(new LiteralControl(bean.EndTime));
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Controls.Add(new LiteralControl(bean.LivingContent));
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Controls.Add(new LiteralControl(bean.LivingName));
                row.Cells.Add(cell);

                HiddenField hidItemId = new HiddenField();
                hidItemId.ID = string.Format("hidItemId_{0}", i);
                hidItemId.Value = Convert.ToString(bean.LivingId);
                TextBox txtPrice = new TextBox();
                txtPrice.ID = string.Format("txtPrice_{0}", i);
                txtPrice.Text = Convert.ToDecimal(bean.LivingPrice) == 0?"":Convert.ToString(decimal.Round(Convert.ToDecimal(bean.LivingPrice), 2));
                txtPrice.MaxLength = 50;
                txtPrice.Width = 100;
                txtPrice.Attributes.Add("onblur", "sumamount('" + i + "')");
                cell = new TableCell();
                cell.Controls.Add(hidItemId);
                cell.Controls.Add(txtPrice);
                row.Cells.Add(cell);

                TextBox txtMultiple = new TextBox();
                txtMultiple.ID = string.Format("txtMultiple_{0}", i);
                txtMultiple.Text = Convert.ToString(bean.LivingMultiple);
                txtMultiple.MaxLength = 50;
                txtMultiple.Width = 100;
                txtMultiple.Attributes.Add("onblur", "sumamount('" + i + "')");
                cell = new TableCell();
                cell.Controls.Add(txtMultiple);
                row.Cells.Add(cell);

                TextBox txtAmount = new TextBox();
                txtAmount.ID = string.Format("txtAmount_{0}", i);
                txtAmount.Text = Convert.ToDecimal(bean.LivingAmount) == 0 ? "" : Convert.ToString(decimal.Round(Convert.ToDecimal(bean.LivingAmount), 2));
                txtAmount.MaxLength = 50;
                txtAmount.Width = 100;
                cell = new TableCell();
                cell.Controls.Add(txtAmount);
                row.Cells.Add(cell);

                TblLiving.Rows.Add(row);
                i++;
            }
        }

        beanFee.MemberNo = Convert.ToString(this.ViewState["MEMBER_NO"]);
        IList lstFee = dao.ListFee(beanFee);
        if (lstFee.Count == 0)
        {
        }
        else
        {
            ViewState[FEE_COUNT] = lstFee.Count;
            int i = 0;
            foreach (LivingMemberFeeBean bean in lstFee)
            {
                TableRow row = new TableRow();
                TableCell cell = new TableCell();

                cell = new TableCell();
                CheckBox chkChooseFee = new CheckBox();
                chkChooseFee.Checked = (Boolean)bean.ChooseFlg;
                chkChooseFee.ID = string.Format("chkChooseFee_{0}", i);
                cell.Controls.Add(chkChooseFee);
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Controls.Add(new LiteralControl(bean.FeeContent));
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Controls.Add(new LiteralControl(bean.FeeName));
                row.Cells.Add(cell);

                HiddenField hidItemIdFee = new HiddenField();
                hidItemIdFee.ID = string.Format("hidItemIdFee_{0}", i);
                hidItemIdFee.Value = Convert.ToString(bean.FeeId);
                TextBox txtPriceFee = new TextBox();
                txtPriceFee.ID = string.Format("txtPriceFee_{0}", i);
                txtPriceFee.Text = Convert.ToDecimal(bean.FeePrice) == 0 ? "" : Convert.ToString(decimal.Round(Convert.ToDecimal(bean.FeePrice), 2));
                txtPriceFee.MaxLength = 50;
                txtPriceFee.Width = 100;
                txtPriceFee.Attributes.Add("onblur", "sumamountfee('" + i + "')");
                cell = new TableCell();
                cell.Controls.Add(hidItemIdFee);
                cell.Controls.Add(txtPriceFee);
                row.Cells.Add(cell);

                TextBox txtMultipleFee = new TextBox();
                txtMultipleFee.ID = string.Format("txtMultipleFee_{0}", i);
                txtMultipleFee.Text = Convert.ToString(bean.FeeMultiple);
                txtMultipleFee.MaxLength = 50;
                txtMultipleFee.Width = 100;
                txtMultipleFee.Attributes.Add("onblur", "sumamountfee('" + i + "')");
                cell = new TableCell();
                cell.Controls.Add(txtMultipleFee);
                row.Cells.Add(cell);

                TextBox txtAmountFee = new TextBox();
                txtAmountFee.ID = string.Format("txtAmountFee_{0}", i);
                txtAmountFee.Text = Convert.ToDecimal(bean.FeeAmount) == 0 ? "" : Convert.ToString(decimal.Round(Convert.ToDecimal(bean.FeeAmount), 2));
                txtAmountFee.MaxLength = 5;
                txtAmountFee.Width = 100;
                cell = new TableCell();
                cell.Controls.Add(txtAmountFee);
                row.Cells.Add(cell);

                TblFee.Rows.Add(row);
                i++;
            }
        }
    }

    /*名称： btnOk_Click
     *功能： 登记当前编辑的会员生活记录。
     *参数： sender - 调用来源对象
     *       e - 包含事件数据的 EventArgs 对象
     *返回： 无
     *说明： 无
     */
    protected void btnOk_Click(object sender, EventArgs e)
    {
        if (ViewState.IsItemDirty(LIVING_COUNT) && ViewState.IsItemDirty(FEE_COUNT))
        {
            IList lstLiving = new ArrayList(); ;
            IList lstFee = new ArrayList(); ;
            ComValidateChecker validator = new ComValidateChecker();

            for (int i = 1; i < TblLiving.Rows.Count; i++)
            {
                CheckBox chkUse = (CheckBox)TblLiving.Rows[i].Cells[0].FindControl(string.Format("chkChoose_{0}", i - 1));
                TextBox txtPrice = (TextBox)TblLiving.Rows[i].Cells[5].FindControl(string.Format("txtPrice_{0}", i - 1));
                TextBox txtMultiple = (TextBox)TblLiving.Rows[i].Cells[6].FindControl(string.Format("txtMultiple_{0}", i - 1));
                TextBox txtAmount = (TextBox)TblLiving.Rows[i].Cells[7].FindControl(string.Format("txtAmount_{0}", i - 1));
                if (chkUse.Checked)
                {
                    // 选定的单价是否为2位小数
                    validator.Add(new CheckDouble(txtPrice.Text, 9, 2, new ComMessage("E0064", new string[] { "单价", "9", "2" })));
                    // 选定的金额是否为2位小数
                    validator.Add(new CheckDouble(txtAmount.Text, 9, 2, new ComMessage("E0064", new string[] { "金额", "9", "2" })));
                    // 选定的月倍数是否为数值
                    validator.Add(new CheckLong(txtMultiple.Text, new ComMessage("E0322", "月倍数")));
                }
            }

            for (int i = 1; i < TblFee.Rows.Count; i++)
            {
                CheckBox chkUseFee = (CheckBox)TblFee.Rows[i].Cells[0].FindControl(string.Format("chkChooseFee_{0}", i - 1));
                TextBox txtPriceFee = (TextBox)TblFee.Rows[i].Cells[3].FindControl(string.Format("txtPriceFee_{0}", i - 1));
                TextBox txtMultipleFee = (TextBox)TblFee.Rows[i].Cells[4].FindControl(string.Format("txtMultipleFee_{0}", i - 1));
                TextBox txtAmountFee = (TextBox)TblFee.Rows[i].Cells[5].FindControl(string.Format("txtAmountFee_{0}", i - 1));
                if (chkUseFee.Checked)
                {
                    // 选定的单价是否为2位小数
                    validator.Add(new CheckDouble(txtPriceFee.Text, 9, 2, new ComMessage("E0064", new string[] { "单价", "9", "2" })));
                    // 选定的金额是否为2位小数
                    validator.Add(new CheckDouble(txtAmountFee.Text, 9, 2, new ComMessage("E0064", new string[] { "金额", "9", "2" })));
                    // 选定的月倍数是否为数值
                    validator.Add(new CheckLong(txtMultipleFee.Text, new ComMessage("E0322", "月倍数")));
                }
            }

            if (validator.DoValidator() == false)
            {
                // 输出校验失败信息
                ((Design_MasterPage)this.Master).InfoMessage = validator.ErrorMessage;
                return;
            }

            for (int i = 1; i < TblLiving.Rows.Count; i++)
            {
                CheckBox chkUse = (CheckBox)TblLiving.Rows[i].Cells[0].FindControl(string.Format("chkChoose_{0}", i - 1));
                if (chkUse.Checked)
                {
                    HiddenField hidItemId = (HiddenField)TblLiving.Rows[i].Cells[5].FindControl(string.Format("hidItemId_{0}", i - 1));
                    TextBox txtPrice = (TextBox)TblLiving.Rows[i].Cells[5].FindControl(string.Format("txtPrice_{0}", i - 1));
                    TextBox txtMultiple = (TextBox)TblLiving.Rows[i].Cells[6].FindControl(string.Format("txtMultiple_{0}", i - 1));
                    TextBox txtAmount = (TextBox)TblLiving.Rows[i].Cells[7].FindControl(string.Format("txtAmount_{0}", i - 1));
                    LivingMemberLivingBean bean = new LivingMemberLivingBean();
                    bean.LivingId = (int)Convert.ToInt32(hidItemId.Value);
                    bean.LivingPrice = txtPrice.Text == "" ? 0 : Convert.ToDecimal(txtPrice.Text);
                    bean.LivingMultiple = txtMultiple.Text == "" ? 0 : Convert.ToInt32(txtMultiple.Text);
                    bean.LivingAmount = txtAmount.Text == "" ? 0 : Convert.ToDecimal(txtAmount.Text);
                    bean.ChooseFlg = true;
                    lstLiving.Add(bean);
                }
            }

            for (int i = 1; i < TblFee.Rows.Count; i++)
            {
                CheckBox chkUseFee = (CheckBox)TblFee.Rows[i].Cells[0].FindControl(string.Format("chkChooseFee_{0}", i - 1));
                if (chkUseFee.Checked)
                {
                    HiddenField hidItemIdFee = (HiddenField)TblFee.Rows[i].Cells[3].FindControl(string.Format("hidItemIdFee_{0}", i - 1));
                    TextBox txtPriceFee = (TextBox)TblFee.Rows[i].Cells[3].FindControl(string.Format("txtPriceFee_{0}", i - 1));
                    TextBox txtMultipleFee = (TextBox)TblFee.Rows[i].Cells[4].FindControl(string.Format("txtMultipleFee_{0}", i - 1));
                    TextBox txtAmountFee = (TextBox)TblFee.Rows[i].Cells[5].FindControl(string.Format("txtAmountFee_{0}", i - 1));
                    LivingMemberFeeBean bean = new LivingMemberFeeBean();
                    bean.FeeId = (int)Convert.ToInt32(hidItemIdFee.Value);
                    bean.FeePrice = txtPriceFee.Text == "" ? 0 : Convert.ToDecimal(txtPriceFee.Text);
                    bean.FeeMultiple = txtMultipleFee.Text == "" ? 0 : Convert.ToInt32(txtMultipleFee.Text);
                    bean.FeeAmount = txtAmountFee.Text == "" ? 0 : Convert.ToDecimal(txtAmountFee.Text);
                    bean.ChooseFlg = true;
                    lstFee.Add(bean);
                }
            }

            LivingMemberLivingDao dao = new LivingMemberLivingDao();
            string userId = Convert.ToString(this.ViewState["USER_ID"]);
            string memberNo = Convert.ToString(this.ViewState["MEMBER_NO"]);
            dao.Insert(lstLiving, lstFee, memberNo, userId);
            ((Design_MasterPage)this.Master).InfoMessage =
                    HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0002").ToString();
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        string directUrl = "~/App/System/PortalMainForm.aspx";
        if (!string.IsNullOrEmpty(Convert.ToString(this.ViewState["URL"])))
        {
            directUrl = Convert.ToString(this.ViewState["URL"]);
        }
        ControllerUtils.Transfer(this.Context, directUrl, false);
    }
}
